package com.mall.order.mq;

import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.Stock;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dal.persistence.StockMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.List;

/**
 * 订单超时未付款消息消费者
 * @author 晓帆
 * @version 1.0
 **/
@Component
@Slf4j
public class DelayOrderConsumer {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    StockMapper stockMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    private DefaultMQPushConsumer consumer;
    @PostConstruct
    public void init(){
        consumer=new DefaultMQPushConsumer("order-consumer");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        try {
            consumer.subscribe("delay_order_topic","*");
            consumer.setMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                    //1. 从消息中取出订单id
                    //2. 查新orderId对应的订单信息，如果订单已经成功支付，什么都不做
                    //3. 如果订单的状态，判断如果订单状态是未支付，订单取消
                    //    a. 修改订单状态，为已取消
                    //    b. 还原锁定库存
                    for (MessageExt messageExt : list) {
                        byte[] body = messageExt.getBody();
                        String orderId = new String(body, 0, body.length);
                        Example example = new Example(Order.class);
                        example.createCriteria().andEqualTo("orderId",orderId);
                        List<Order> orders = orderMapper.selectByExample(example);
                        Order order = orders.get(0);
                        if (order.getStatus()==0){
                            Example example1 = new Example(OrderItem.class);
                            example1.createCriteria().andEqualTo("orderId",orderId);
                            List<OrderItem> orderItems = orderItemMapper.selectByExample(example1);
                            for (OrderItem orderItem : orderItems) {
                                //获取还原锁定库存的商品id和还原的数量
                                Long itemId = orderItem.getItemId();
                                System.out.println(itemId);
                                Integer num = orderItem.getNum();
                                Integer lock_count=-num;
                                Long stock_count =num.longValue();
                                System.out.println(lock_count);
                                System.out.println(stock_count);
                                Stock stock = new Stock();
                                stock.setItemId(itemId);
                                stock.setStockCount(stock_count);
                                stock.setLockCount(lock_count);
                                stockMapper.updateStock(stock);
                                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                            }
                        }
                    }
                    return null;
                }
            });
            consumer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }

    }


}
